//
//  privateChat.m
//  CYWFrame
//
//  Created by Macx on 2017/9/28.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "privateChat.h"
#import "LoginServer.h"

@implementation privateChat

+ (int)chatListUnReadMessageNum {
	
	
	TIMManager *manager =  [TIMManager sharedInstance];
    NSArray * conversations = [manager getConversationList];
	//NSArray *conversations;
	
    int count = 0;
    for (TIMConversation *conversation in conversations) {
        // 如果是c2c就输出
        if (conversation.getType == 1) {
            int num = [conversation getUnReadMessageNum];
//            NSLog(@"未读消息 = %d", num);
            count = count + num;
        }
    }
	/*
	TIMManager *manager =  [TIMManager sharedInstance];
	int count = [manager ConversationCount];
	
	for (int i = 0; i<count; i++) {
		TIMConversation *conversation = [[TIMManager sharedInstance] getConversationByIndex:i];
		if (conversation.getType == 1) {
			int num = [conversation getUnReadMessageNum];
			count = count + num;
		}
	}
	 */
    return count;
}

+ (void)chatListConversationBlock:(void (^)(NSMutableArray *, NSMutableArray *))block {
    
//    [[TIMManager sharedInstance] deleteConversation:TIM_C2C receiver:@"101399"];
    NSArray * conversations = [[TIMManager sharedInstance] getConversationList];
    
    
    NSMutableArray *conversationArr = [[NSMutableArray alloc] init];
    NSMutableArray *chatListArr = [[NSMutableArray alloc] init];
    
    
    for (int i = 0; i < conversations.count; i++) {
        TIMConversation *conversation = conversations[i];
        
        NSString *ppp = [conversation getReceiver];
        NSLog(@"pppppp = %@", ppp);
        
        NSInteger count = 0;
        count = conversations.count;
        
        // 如果是c2c就输出
        if (conversation.getType == 1) {
            
            [conversationArr addObject:conversation];
            NSMutableArray *listArr = [[NSMutableArray alloc] init];
            
            NSString *unR = [NSString stringWithFormat:@"%d", [conversation getUnReadMessageNum]];
//            NSLog(@"unR = %@", unR);
//            if (!unR) {
//                unR = @"0";
//            }
            
            [conversation getMessage:1 last:nil succ:^(NSArray *msgs) {
                
                for (TIMMessage *msg in msgs) {
                    
                    NSLog(@"count %d", [msg elemCount]);
                    
                    //                    for(int index = 0; index < [msg elemCount]; index++) {
                    TIMElem *elem = [msg getElem:0];
                    
                    
                    if([elem isKindOfClass:[TIMTextElem class]]) {
                        // 消息总入口频率限制
                        static TCFrequeControl *freqControl = nil;
                        if (freqControl == nil) {
                            freqControl = [[TCFrequeControl alloc] initWithCounts:20 andSeconds:1];
                        }
                        if (![freqControl canTrigger]) {
                            return;
                        }
                        //                            if (msg.isSelf) {
                        //                                return;
                        //                            }
                        // 文本消息
                        TIMTextElem *textElem = (TIMTextElem *)elem;
                        NSString *msgText = textElem.text;
                        
                        [listArr addObject:msgText];
                        
                    }
                    else if ([elem isKindOfClass:[TIMImageElem class]]) {
                        //                            if (msg.isSelf) {
                        //                                return;
                        //                            }
                        [listArr addObject:@"[图片]"];
                    }
                    else if ([elem isKindOfClass:[TIMSoundElem class]]) {}
                    else if ([elem isKindOfClass:[TIMVideoElem class]]) {
                        //                            if (msg.isSelf) {
                        //                                return;
                        //                            }
                        [listArr addObject:@"[视频]"];
                    }
                    else if ([elem isKindOfClass:[TIMFileElem class]]) {}
                    else if([elem isKindOfClass:[TIMCustomElem class]]) {
                        //                            if (msg.isSelf) {
                        //                                return;
                        //                            }
                        // 自定义消息
                        TIMCustomElem* cele = (TIMCustomElem*)elem;
                        NSString *dataStr = [[NSString alloc] initWithData:cele.data encoding:NSUTF8StringEncoding];
                        DebugLog(@"datastr is:%@", dataStr);
                    }
                    else {
                        [listArr addObject:@" "];
                    }
                    
                    [listArr addObject:ppp];
                    // 发送者资料
                    //                        [listArr addObject:[msg GetSenderProfile]];
                    [listArr addObject:unR];
                    
                    NSLog(@"list = %@", listArr);
                    
                    [chatListArr addObject:listArr]; // 信息  userId 未读数量
                }
                if (i == conversations.count - 1) {
                    block(conversationArr, chatListArr);
                }
            } fail:^(int code, NSString *msg) {
                NSLog(@"%d, %@", code, msg);
            }];
        }
        else
            count--;
    }
    
}

+ (void)chatListArr:(NSMutableArray *)chatListArr tableView:(UITableView *)tableView {
    
    NSArray * conversations = [[TIMManager sharedInstance] getConversationList];
    
//    [conversationArr removeAllObjects];
    [chatListArr removeAllObjects];
    for (TIMConversation *conversation in conversations) {
        
        // 如果是c2c就输出
        if (conversation.getType == 1) {
            NSLog(@"%@", conversation);
            
//            [conversationArr addObject:conversation];
            NSMutableArray *listArr = [[NSMutableArray alloc] init];
            
            
            //            NSLog(@"未读消息 = %d", [conversation getUnReadMessageNum]);
            
            NSString *unR = [NSString stringWithFormat:@"%d", [conversation getUnReadMessageNum]];
            
            [conversation getMessage:1 last:nil succ:^(NSArray *msgs) {
                
                for (TIMMessage *msg in msgs) {
                    //                    NSLog(@"====%@", msg);
                    for(int index = 0; index < [msg elemCount]; index++) {
                        TIMElem *elem = [msg getElem:index];
                        
                        if([elem isKindOfClass:[TIMTextElem class]]) {
                            // 消息总入口频率限制
                            static TCFrequeControl *freqControl = nil;
                            if (freqControl == nil) {
                                freqControl = [[TCFrequeControl alloc] initWithCounts:20 andSeconds:1];
                            }
                            if (![freqControl canTrigger]) {
                                return;
                            }
                            // 文本消息
                            TIMTextElem *textElem = (TIMTextElem *)elem;
                            // 聊天信息
                            NSString *msgText = textElem.text;
                            
                            [listArr addObject:msgText];
                            
                        }else if ([elem isKindOfClass:[TIMImageElem class]]) {
                            
                            [listArr addObject:@"[图片]"];
                            
                        }else if ([elem isKindOfClass:[TIMSoundElem class]]) {
                            
                            [listArr addObject:@"[语音]"];
                            
                        }else if ([elem isKindOfClass:[TIMVideoElem class]]) {
       
                            [listArr addObject:@"[视频]"];
                            
                        }else if ([elem isKindOfClass:[TIMFileElem class]]) {

                            [listArr addObject:@"[文件]"];
                            
                        }else if([elem isKindOfClass:[TIMCustomElem class]]) {
                            // 自定义消息
                            TIMCustomElem* cele = (TIMCustomElem*)elem;
                            NSString *dataStr = [[NSString alloc] initWithData:cele.data encoding:NSUTF8StringEncoding];
                            DebugLog(@"datastr is:%@", dataStr);
                            if (msg.isSelf) {
                                return;
                            }
                            [listArr addObject:@"[自定义]"];
                        }
                        
                        // 发送者资料
                        if (listArr.count == 0) {
                            return;
                        }
                        [listArr addObject:[msg GetSenderProfile]];
                        [listArr addObject:unR];
                        [chatListArr addObject:listArr];
                        
                    }
                }
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    // 获取用户头像
                    for (NSMutableArray *arr in chatListArr) {
                        TIMUserProfile *profile = arr[1];
                        [[NetworkTool sharedTool] requestWithName:@"获取用户信息" URLString:APIUser parameters:@{@"uid":profile.identifier} method:@"GET" callBack:^(id responseObject) {
                            profile.faceURL = [responseObject[@"data"] valueForKey:@"face"];                            profile.nickname = [responseObject[@"data"] valueForKey:@"nickname"];
                            arr[1] = profile;
                            dispatch_async(dispatch_get_main_queue(), ^{
                                [tableView reloadData];
                            });
                        }];
                    }
                });
            } fail:^(int code, NSString *msg) {
                NSLog(@"%d, %@", code, msg);
            }];
        }
    }
    
}


/*
+ (void)chatListConversationArr:(NSMutableArray *)conversationArr chatListArr:(NSMutableArray *)chatListArr block:(void (^)(id, id))block {
    
    NSArray * conversations = [[TIMManager sharedInstance] getConversationList];
    
    [[TIMManager sharedInstance] deleteConversation:TIM_C2C receiver:@"100007"];
    
    [conversationArr removeAllObjects];
    [chatListArr removeAllObjects];
//    for (TIMConversation *conversation in conversations) {
    for (int i = 0; i < conversations.count; i++) {
        TIMConversation *conversation = conversations[i];
        
        NSString *ppp = [conversation getReceiver];
        NSLog(@"pppppp = %@", ppp);
        
        // 如果是c2c就输出
        if (conversation.getType == 1) {
            
            [conversationArr addObject:conversation];
            NSMutableArray *listArr = [[NSMutableArray alloc] init];
            
            NSString *unR = [NSString stringWithFormat:@"%d", [conversation getUnReadMessageNum]];
            
            [conversation getMessage:1 last:nil succ:^(NSArray *msgs) {
                
                for (TIMMessage *msg in msgs) {
                    
//                    TIMUserProfile *ppp = [msg GetSenderProfile];
//                    NSLog(@"pppppp = %@", ppp.identifier);
                    //                    NSLog(@"====%@", msg);
                    for(int index = 0; index < [msg elemCount]; index++) {
                        TIMElem *elem = [msg getElem:index];
                        
                        if([elem isKindOfClass:[TIMTextElem class]]) {
                            // 消息总入口频率限制
                            static TCFrequeControl *freqControl = nil;
                            if (freqControl == nil) {
                                freqControl = [[TCFrequeControl alloc] initWithCounts:20 andSeconds:1];
                            }
                            if (![freqControl canTrigger]) {
                                return;
                            }
                            // 文本消息
                            TIMTextElem *textElem = (TIMTextElem *)elem;
                            NSString *msgText = textElem.text;
                            
                            [listArr addObject:msgText];
                            
                        }
                        else if ([elem isKindOfClass:[TIMImageElem class]]) {
                            [listArr addObject:@"[图片]"];
                        }
                        else if ([elem isKindOfClass:[TIMSoundElem class]]) {}
                        else if ([elem isKindOfClass:[TIMVideoElem class]]) {
                            [listArr addObject:@"[视频]"];
                        }
                        else if ([elem isKindOfClass:[TIMFileElem class]]) {}
                        else if([elem isKindOfClass:[TIMCustomElem class]]) {
                            // 自定义消息
                            TIMCustomElem* cele = (TIMCustomElem*)elem;
                            NSString *dataStr = [[NSString alloc] initWithData:cele.data encoding:NSUTF8StringEncoding];
                            DebugLog(@"datastr is:%@", dataStr);
                        }
                        
                        [listArr addObject:ppp];
                        [listArr addObject:unR];
                        
                        [chatListArr addObject:listArr]; // 信息  userId 未读数量
                    }
                }
                if (i == conversations.count - 1) {
                    block(conversationArr, chatListArr);
                }
            } fail:^(int code, NSString *msg) {
                NSLog(@"%d, %@", code, msg);
            }];
        }
    }
    
}*/

@end
